package com.examples.gateway.helper.filter;

import com.examples.core.req.ExamHeader;
import com.examples.gateway.helper.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 添加用户信息到请求头过滤器
 * @author Dai YaLu
 */
@Slf4j
public class AddPermHeaderFilter extends AbstractGlobalHelperFilter {

    private static final String USER_ID ="userId";

    @Override
    public Mono<Void> doFilter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ExamHeader user = exchange.getAttribute(Constants.EXAMPLES_GATEWAY_USER_DETAIL);
        if (user != null) {
            exchange.getRequest().mutate().header(USER_ID, user.getUserId().toString()).build();
        }
        return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().build()).build());
    }

    @Override
    public int getOrder() {
        return 51;
    }
}
